{/* Copyright 2025 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License. */}

import {Layout} from '../../src/Layout';
export default Layout;
import docs from 'docs:@react-aria/label';
import {FunctionAPI} from '../../src/FunctionAPI';
import {InterfaceType} from '../../src/types';

export const section = 'Utilities';
export const description = 'Associates a label with a form field for accessibility.';

# useLabel

<PageDescription>{docs.exports.useLabel.description}</PageDescription>

## Example

The `useLabel` hook associates a label with a field. It automatically handles
creating an id for the field and associates the label with it.

```tsx render
'use client';
import {useLabel} from 'react-aria';

function ColorField(props) {
  let {labelProps, fieldProps} = useLabel(props);

  return (
    <>
      <label {...labelProps}>{props.label}</label>
      <select {...fieldProps}>
        <option>Indigo</option>
        <option>Maroon</option>
        <option>Chartreuse</option>
      </select>
    </>
  );
}

<ColorField label="Favorite color" />
```

By default, `useLabel` assumes that the label is a native HTML label element.
However, if you are labeling a non-native form element, be sure to use an
element other than a `<label>` and set the `labelElementType` prop appropriately.

See [useRadioGroup](RadioGroup/useRadioGroup.html#example) and [useTextField](TextField/useTextField.html#example)
for examples of how `useLabel` is used by components.

## API

<FunctionAPI function={docs.exports.useLabel} links={docs.links} />

### LabelAriaProps

<InterfaceType {...docs.exports.LabelAriaProps} />

### LabelAria

<InterfaceType {...docs.exports.LabelAria} />
